﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

public partial class testThumnail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUploadPhoto_Click(object sender, EventArgs e)
    {
        //string pathAvatars = Server.MapPath("~/upload/thumnails/");
        //try
        //{
        //    int thumbWidth = 50;
        //    int thumbHeight = 40;

        //    System.Drawing.Image oImg = System.Drawing.Image.FromStream(fulPhoto.PostedFile.InputStream);
        //    System.Drawing.Image oThumbNail = new Bitmap(thumbWidth, thumbHeight, oImg.PixelFormat);

        //    Graphics oGraphic = Graphics.FromImage(oThumbNail);
        //    oGraphic.CompositingQuality = CompositingQuality.HighQuality;
        //    oGraphic.SmoothingMode = SmoothingMode.HighQuality;
        //    oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

        //    Rectangle oRectangle = new Rectangle(0, 0, thumbWidth, thumbHeight);
        //    oGraphic.DrawImage(oImg, oRectangle);

        //    oThumbNail.Save(pathAvatars + "\\Avatar.jpg", ImageFormat.Jpeg);
        //    oImg.Dispose();
        //}
        //catch (Exception ex)
        //{
        //    Response.Write("Error: " + ex.Message);
        //}
        thunhoVaLuu(50, 40, fulPhoto);
    }

    public void thunhoVaLuu(int chieuRong,int chieuCao,FileUpload fulPhoto2)
    {
        string pathAvatars = Server.MapPath("~/upload/thumnails/");
        try
        {
            int thumbWidth = 50;
            int thumbHeight = 40;
            string tenfile = fulPhoto2.PostedFile.FileName;
            System.Drawing.Image oImg = System.Drawing.Image.FromStream(fulPhoto2.PostedFile.InputStream);
            System.Drawing.Image oThumbNail = new Bitmap(thumbWidth, thumbHeight, oImg.PixelFormat);

            Graphics oGraphic = Graphics.FromImage(oThumbNail);
            oGraphic.CompositingQuality = CompositingQuality.HighQuality;
            oGraphic.SmoothingMode = SmoothingMode.HighQuality;
            oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

            Rectangle oRectangle = new Rectangle(0, 0, thumbWidth, thumbHeight);
            oGraphic.DrawImage(oImg, oRectangle);

            oThumbNail.Save(pathAvatars + tenfile, ImageFormat.Jpeg);
            oImg.Dispose();
        }
        catch (Exception ex)
        {
            Response.Write("Error: " + ex.Message);
        }
    }
}
